﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MulticastTV
{
    /// <summary>
    /// Obsolete
    /// </summary>
    public class GlobalMouse:NativeWindow
    {
        private const int WM_SETCURSOR = 0x20;

        /// <summary>
        /// The WM_MOUSEMOVE message is posted to a window when the cursor moves. 
        /// </summary>
        private const int WM_MOUSEMOVE      = 0x200;
        /// <summary>
        /// The WM_LBUTTONDOWN message is posted when the user presses the left mouse button 
        /// </summary>
        private const int WM_LBUTTONDOWN    = 0x201;
        /// <summary>
        /// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button
        /// </summary>
        private const int WM_RBUTTONDOWN    = 0x204;
        /// <summary>
        /// The WM_MBUTTONDOWN message is posted when the user presses the middle mouse button 
        /// </summary>
        private const int WM_MBUTTONDOWN    = 0x207;
        /// <summary>
        /// The WM_LBUTTONUP message is posted when the user releases the left mouse button 
        /// </summary>
        private const int WM_LBUTTONUP      = 0x202;
        /// <summary>
        /// The WM_RBUTTONUP message is posted when the user releases the right mouse button 
        /// </summary>
        private const int WM_RBUTTONUP      = 0x205;
        /// <summary>
        /// The WM_MBUTTONUP message is posted when the user releases the middle mouse button 
        /// </summary>
        private const int WM_MBUTTONUP      = 0x208;
        /// <summary>
        /// The WM_LBUTTONDBLCLK message is posted when the user double-clicks the left mouse button 
        /// </summary>
        private const int WM_LBUTTONDBLCLK  = 0x203;
        /// <summary>
        /// The WM_RBUTTONDBLCLK message is posted when the user double-clicks the right mouse button 
        /// </summary>
        private const int WM_RBUTTONDBLCLK  = 0x206;
        /// <summary>
        /// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button 
        /// </summary>
        private const int WM_MBUTTONDBLCLK  = 0x209;
        /// <summary>
        /// The WM_MOUSEWHEEL message is posted when the user presses the mouse wheel. 
        /// </summary>
        private const int WM_MOUSEWHEEL     = 0x020A;

        private Form1 parent;

        public GlobalMouse(Form1 parent)
        {
            parent.HandleCreated += new EventHandler(this.OnHandleCreated);
            parent.HandleDestroyed += new EventHandler(this.OnHandleDestroyed);
            this.parent = parent;
        }

        // Listen for the control's window creation and then hook into it.
        internal void OnHandleCreated(object sender, EventArgs e)
        {
            // Window is now created, assign handle to NativeWindow.
            AssignHandle(((Form1)sender).Handle);
        }
        internal void OnHandleDestroyed(object sender, EventArgs e)
        {
            // Window was destroyed, release hook.
            ReleaseHandle();
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
              
            bool handled = false;

            //detect button clicked
            System.Windows.Forms.MouseButtons button = System.Windows.Forms.MouseButtons.None;
           
            switch ((int)m.WParam)
            {
                case WM_LBUTTONUP:
                    button = MouseButtons.Left;
                    break;
                case WM_LBUTTONDOWN:
                    //case WM_LBUTTONUP: 
                    //case WM_LBUTTONDBLCLK: 
                    button = MouseButtons.Left;
                    break;
                case WM_RBUTTONUP:
                    button = MouseButtons.Right;
                    break;
                case WM_RBUTTONDOWN:
                    //case WM_RBUTTONUP: 
                    //case WM_RBUTTONDBLCLK: 
                    button = MouseButtons.Right;
                    break;
            }
            
            //double clicks
            int clickCount = 1;
            if (button != MouseButtons.None)
                if ((int)m.WParam == WM_LBUTTONDBLCLK || (int)m.WParam == WM_RBUTTONDBLCLK)
                    clickCount = 2;

            //generate event 
            GlobalMouseEventArgs e = new GlobalMouseEventArgs(
                                                false,
                                                button,
                                                clickCount,
                                                HiWord(m.LParam.ToInt32()),
                                                LoWord(m.LParam.ToInt32()),
                                                0);
            //raise it
            if ((int)m.Msg == 0xA0)
            {
                int test = 1;
                //OnMouseActivity(this, e);
            }
            else if ((int)m.WParam == WM_LBUTTONUP)
            {
               // OnMouseUp(this, e);
            }
            else if ((int)m.WParam == WM_LBUTTONDOWN)
            {
               /// OnMouseDown(this, e);
            }
            else if ((int)m.WParam == WM_MOUSEWHEEL)
            {
               // OnMouseWheel(this, e);
            }
            else if ((int)m.WParam == WM_RBUTTONDOWN)
            {
              //  OnRightMouseDown(this, e);
            }

              handled = e.Handled;
              if (handled)
              {
                  m.Msg = 0;
                  return;
              }

            base.WndProc(ref m);
        }

        private static int HiWord(int number)
        {
            if ((number & 0x80000000) == 0x80000000)
                return (number >> 16);
            else
                return (number >> 16) & 0xffff;
        }

        private static int LoWord(int number)
        {
            return number & 0xffff;
        }

    }

    public class GlobalMouseEventArgs : MouseEventArgs
    {
        public bool Handled = false;

        public GlobalMouseEventArgs(Boolean handled, MouseButtons button, int clicks, int x, int y,
        int delta)
            : base(button, clicks, x, y, delta)
        {
            Handled = handled;
        }
    }
}
